package com.dartfar.base.action;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.Date;

import org.apache.struts2.ServletActionContext;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import com.dartfar.base.vo.UploadVo;

/**
 * 文件上传Action
 * @author 王琦
 */
@Component("fileUploadAction")
@Scope("prototype")
public class FileUploadAction extends BaseAction {


	 /**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
	private static final int BUFFER_SIZE = 20 * 1024; // 20K  

    private File file;  
    private String fileFileName;  
    private String fileFileContentType;  
  
    private String message = "上传图片成功";
    
    private String attPath;	//附件路径
    private String attName;	//附件名称
    
    public String test(){
    	String path = ServletActionContext.getServletContext().getRealPath("/");
    	System.out.println(path);
    	return null ;
    }
    
	public String upload() throws Exception{  
		
		String newFileName = new Date().getTime() + getExtention(fileFileName);
		
		String savePath = ServletActionContext.getServletContext().getRealPath("/upload");

        File f_path = new File(savePath);
    	if (!f_path.exists()) {
    		f_path.mkdirs();
    	}
		
		System.out.println("savePath: " + savePath);
		System.out.println("savePath: " + newFileName);
		try {  
            File file = this.getFile();  
            if(fileFileName.endsWith(".exe")){  
                message="文件错误!";  
                return ERROR;  
            }  
            FileInputStream fis = new FileInputStream(file);  
            FileOutputStream fos = new FileOutputStream(savePath + "/" + newFileName);  
            byte[] buf = new byte[1024];  
            int length = 0;  
            while ((length = fis.read(buf)) != -1) {  
            	fos.write(buf, 0, length);  
            }  
            fis.close();  
            fos.flush();  
            
            
            attName = fileFileName;
            attPath = "upload/" + newFileName;

            message = "上传成功";
        } catch (Exception e) {  
            e.printStackTrace();  
            message = "上传异常!";  
        }  

        return SUCCESS;
    }  
      
    private static String getExtention(String fileName) {  
        int pos = fileName.lastIndexOf(".");  
  
        return fileName.substring(pos);  
    }

	public File getFile() {
		return file;
	}

	public void setFile(File file) {
		this.file = file;
	}

	public String getFileFileName() {
		return fileFileName;
	}

	public void setFileFileName(String fileFileName) {
		this.fileFileName = fileFileName;
	}

	public String getFileFileContentType() {
		return fileFileContentType;
	}

	public void setFileFileContentType(String fileFileContentType) {
		this.fileFileContentType = fileFileContentType;
	}

	public String getMessage() {
		return message;
	}

	public void setMessage(String message) {
		this.message = message;
	}

	public String getAttPath() {
		return attPath;
	}

	public void setAttPath(String attPath) {
		this.attPath = attPath;
	}

	public String getAttName() {
		return attName;
	}

	public void setAttName(String attName) {
		this.attName = attName;
	}

	
}
